package com.dgshanger.sanhxiaofeisc.ui

import android.text.TextUtils
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.xx.baseuilibrary.mvp.BaseMvpViewActivity
import kotlinx.android.synthetic.main.activity_web.*
import org.jsoup.Jsoup


/**
 * WebActivity
 * 沉迷学习不能自拔
 * Describe：网页显示通用页面
 * Created by 雷小星🍀 on 2018/3/16 10:04.
 */
class WebActivity : BaseMvpViewActivity() {

    override fun getActivityLayoutId(): Int = R.layout.activity_web


    override fun initEvent() {
        ib_back.setOnClickListener { finish() }
    }

    override fun initData() {
//
//        webview.setHorizontalScrollBarEnabled(false)
//        webview.setVerticalScrollBarEnabled(false)
//        webview.setScrollContainer(false)
//        with(webview.settings) {
//            setSupportZoom(false)
//            setJavaScriptEnabled(true)
//            setBlockNetworkImage(false)
//            setUseWideViewPort(false)
//            setLayoutAlgorithm(android.webkit.WebSettings.LayoutAlgorithm.SINGLE_COLUMN)
//            setLayoutAlgorithm(android.webkit.WebSettings.LayoutAlgorithm.NARROW_COLUMNS)
//            setBuiltInZoomControls(false) // 设置显示缩放按钮
//            setSupportZoom(false) // 支持缩放
//            if (android.os.Build.VERSION.SDK_INT >= 21)
//            //Build.VERSION_CODES.LOLLIPOP
//                setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
//
//            webview.webViewClient = object : WebViewClient() {
//
//                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
//                    return super.shouldOverrideUrlLoading(view, url)
//                }
//
//                override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
//                    return super.shouldOverrideUrlLoading(view, request)
//                }
//            }
//        }


        //设置标题
        tv_title.text = getWebTitle()
        if (!TextUtils.isEmpty(getData())) {
            //加载数据
            webview.loadDataWithBaseURL(Constants.getBaseUrl(), getNewContent(getData()!!), "text/html", "utf-8", null)
            return
        }
        val url = getUrl()
        if (!TextUtils.isEmpty(url)) {
            //加载链接
            webview.loadUrl(url)
            return
        }

        showToast("没有可以打开的资源")
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        webview.reload()
        webview.removeAllViews()
        webview.destroy()
    }

    private fun getNewContent(htmlText: String): String {
        val doc = Jsoup.parse(htmlText)
        val elements = doc.getElementsByTag("img")
        for (element in elements) {
            element.attr("width", "100%").attr("height", "auto")
        }
        return doc.toString()
    }

    private fun getWebTitle(): String? = intent.getStringExtra("title")

    private fun getData(): String? = intent.getStringExtra("data")

    private fun getUrl(): String? = intent.getStringExtra("url")


}
